package com.mojo.concurrent.concurrent_07_pool;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 自定义线程工厂
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/4/27 16:34
 */
public class MyThreadFactory implements ThreadFactory {
    final static AtomicInteger poolNumber = new AtomicInteger(1);
    final static AtomicInteger threadNumber = new AtomicInteger(1);
    private final String namePrefix;

    public MyThreadFactory() {
        namePrefix = "poll-" + poolNumber.getAndIncrement() + "-thread-";
    }

    @Override
    public Thread newThread(Runnable r) {
        int andDecrement = threadNumber.getAndIncrement();
        return new Thread(r, namePrefix + andDecrement);
    }
}
